﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using RegExStudio.RegExStudioEventArgs;

namespace RegExStudio.UserControls
{
    /// <summary>
    /// Control das das RegEx Pattern anzeigt / ändert
    /// </summary>
    public partial class CtrlPattern : CtrlGroup
    {
        private bool _warningVisible = false;

        /// <summary>
        /// Wenn es eine Exception beim RegEx erzeigen gab = true, sonst false
        /// Wenn true, wird das Control mit der Warnungfarbe eingefärbt
        /// </summary>
        public bool WarningVisible
        {
            get { return _warningVisible; }
            set 
            {
                _warningVisible = value;
                if (_warningVisible)
                {
                    grpGroup.BackColor = Properties.Settings.Default.PatternExceptionColor;
                }
                else
                {
                    grpGroup.BackColor = this.BackColor;
                }
            }
        }

        /// <summary>
        /// Das RegEx Pattern
        /// </summary>
        public string Pattern
        {
            get { return txtPattern.Text; }
            set { txtPattern.Text = value; }
        }

        /// <summary>
        /// Die RegEx Optionen
        /// </summary>
        public RegexOptions Options
        {
            get { return ctrlOptions.Options; }
            set { ctrlOptions.Options = value; }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public CtrlPattern()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Control wird geladen
        /// Eingabecontrol-Hintergrundfärbung aktivieren
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CtrlPattern_Load(object sender, EventArgs e)
        {
            txtPattern.Enter += new EventHandler(Control_Enter);
            txtPattern.Leave += new EventHandler(Control_Leave);
        }
        
        /// <summary>
        /// Das Pattern wurde geändert
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPattern_TextChanged(object sender, EventArgs e)
        {
            OnPatternChanged(txtPattern.Text);
        }

        /// <summary>
        /// EventHandler für Änderung des Patterns
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void PatternChangedEventHandler(object sender, PatternChangedEventArgs e);

        /// <summary>
        /// Event das ausgelöst wird, wenn das Pattern geändert wurde
        /// </summary>
        public event PatternChangedEventHandler PatternChanged;

        /// <summary>
        /// Feuert das Event, wenn das Pattern geändert wurde
        /// </summary>
        /// <param name="pattern"></param>
        protected virtual void OnPatternChanged(string pattern)
        {
            if (PatternChanged != null)
            {
                PatternChanged(this, new PatternChangedEventArgs(pattern));
            }
        }

        /// <summary>
        /// Die Optionen wurde geändert
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctrlOptions_OptionsChanged(object sender, OptionsChangedEventArgs e)
        {
            OnOptionsChanged(e.Options);
        }

        /// <summary>
        /// EventHandler für Änderung der Optionen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void OptionsChangedEventHandler(object sender, OptionsChangedEventArgs e);

        /// <summary>
        /// Event das ausgelöst wird, wenn die Optionen geändert wurden
        /// </summary>
        public event OptionsChangedEventHandler OptionsChanged;

        /// <summary>
        /// Feuert das Event, wenn die Optionen geändert wurden
        /// </summary>
        /// <param name="options"></param>
        protected virtual void OnOptionsChanged(RegexOptions options)
        {
            if (OptionsChanged != null)
            {
                OptionsChanged(this, new OptionsChangedEventArgs(options));
            }
        }
        
    }
}
